package com.foodmarket.controller;
import com.foodmarket.pojo.Result;
import com.foodmarket.pojo.Sellers;
import com.foodmarket.service.UserService;
import com.foodmarket.utils.JwtUtil;
import com.foodmarket.utils.MD5Util;
import com.foodmarket.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/seller")
public class SellerController {
    @Autowired
    private UserService userService;
    @PostMapping("/register") // 注册卖家
    public Result registerSeller(String username, String password, String phone, String email,String name) {
        // 查询用户
        if(username.equals("")||password.equals("")||phone.equals("")||email.equals("")||name.equals(""))
        {
            return Result.error("请输入完整的数据");
        }
        Sellers seller = userService.findBySellerName(username);
        if (seller == null) {
            // 用户不存在，进行注册
            userService.registerSeller(username, password, phone,email,name);
            return Result.success("卖家注册成功");
        } else {
            // 用户名已存在
            return Result.error("用户名已或手机号被占用");
        }
    }
    @PostMapping("/login")
    public Result<String> sellerLogin(String username, String password){
        //根据用户名查询用户
        Sellers loginSeller = userService.findBySellerName(username);
        //判断用户名存在
        if(username.equals("")||password.equals(""))
        {
            return Result.error("请输入完整的信息");
        }
        if (loginSeller==null){
            return Result.error("用户名不存在");
        }
        //判断密码是否正确
        if (MD5Util.getMD5String(password).equals(loginSeller.getPassword())){

            Map<String,Object> claims=new HashMap<>();
            claims.put("id",loginSeller.getSellerId());
            claims.put("username",loginSeller.getUsername());
            String token = JwtUtil.genToken(claims);
            //密码正确
            return Result.success(token);
        }
        return Result.error("密码错误");
    }
    @GetMapping("/sellerInfo")
    public Result<Sellers> userInfo( ){
        //根据用户名查询用户
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Sellers seller=userService.findBySellerName(username);
        return Result.success(seller);
    }

    @PutMapping("update")//更新
    public Result update(@RequestBody Sellers sellers)
    {
        userService.updateSeller(sellers);
        return Result.success();
    }

    @PatchMapping("updateSellerPwd")//修改密码
    public Result updatePwd(@RequestBody Map<String,String> params){
        //校验参数
        String oldSellerPwd=params.get("oldSeller_pwd");
        String newSellerPwd=params.get("newSeller_pwd");
        String reSellerPwd=params.get("reSeller_pwd");

        if (!StringUtils.hasLength(oldSellerPwd)||!StringUtils.hasLength(newSellerPwd)||!StringUtils.hasLength(reSellerPwd)){
            return Result.error("缺少必要的参数");
        }
        //原密码是否正确
        Map<String,Object> mapSeller = ThreadLocalUtil.get();
        String username= (String) mapSeller.get("username");
        Sellers loginSeller=userService.findBySellerName(username);

        if (! loginSeller.getPassword().equals(MD5Util.getMD5String(oldSellerPwd))){
            return Result.error("原密码填写不正确");
        }
        if (!reSellerPwd.equals(newSellerPwd)){
            return Result.error("两次填写的密码不一样");
        }
        //更新

        userService.updateSellerPwd(newSellerPwd);
        return Result.success();
    }

}